<?php
/**
 * Created by PhpStorm.
 * User: 游贤川
 * Date: 2021/9/5
 * Time: 19:04
 */

namespace App\Http\Controllers\Api;


use App\Http\Controllers\Controller;
use App\Models\EatMessage;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Validator;
class EatMessageController extends Controller
{

    public function create(Request $request)
    {
        //验证
        $verify = [
            'open_id' => 'required',
        ];
        $regulation = [
            'code.required' => 'open_id必填',
        ];
        $validator = Validator::make($request->all(), $verify, $regulation);
        if ($validator->fails()) {
            return $this->error($validator->errors()->first());
        }
        $data=$request->input();
        try{
            if (isset($data['str_1']) && isset($data['open_id'])){
                $message = EatMessage::query()->updateOrCreate(['open_id'=>$data['open_id']],$data);
            }else{
                $message = EatMessage::query()->where('open_id',$data['open_id'])->first();
            }
            return $this->success($message,'成功');
        }catch (\Exception $exception){
            Log::error('试吃2期调研错误',[$exception->getMessage()]);
            return $this->error($exception->getMessage());
        }

    }

}